﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace AllPurposeCard.XuJian
{

    public partial class PayByCard : Form
    {
        public PayByCard()
        {
            InitializeComponent();
        }
        public const int WM_SYSCOMMAND = 0x112;
        public const int SC_MOVE = 0xF012;
        //禁止拖动
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_SYSCOMMAND)
            {
                if ((int)m.WParam == SC_MOVE)
                    return;
            }
            base.WndProc(ref m);
        }
        //卡号查询登录
        private void Inquire_Click(object sender, EventArgs e)
        {
            //判断卡号是否为数字
            ZhengZeBiaoDa ZZ = new ZhengZeBiaoDa();
            bool result = ZZ.Number(UserCardID.Text);
            if (result)
            {
                for (int i = 0; i < UserCardID.Text.Length; i++)
                {
                    if (char.IsDigit(UserCardID.Text[i]) == false)
                    {
                        MessageBox.Show("请输入合法的卡号", "警告");
                        return;
                    }
                }
                //如果为数字
                DBHelper DB = new DBHelper();

                string sql = string.Format(@"select * from UserInformation where UserCardID = '{0}'", UserCardID.Text);
                DataTable inf = DB.GetDataTableBySql(sql);
                //判断返回标结果集是否为空
                if (inf.Rows.Count != 0)
                {
                    MessageBox.Show("查询成功", "提示");
                    UserCardID.Text = inf.Rows[0]["UserCardID"].ToString();
                    UserAge.Text = inf.Rows[0]["UserAge"].ToString();
                    UserName.Text = inf.Rows[0]["UserName"].ToString();
                    UserSex.Text = inf.Rows[0]["UserSex"].ToString();
                }
                //未查询到用户
                else
                {
                    MessageBox.Show("为查询到此用户，请输入正确卡号", "提示");
                }
            }
            else
            {
                MessageBox.Show("请输入合法的卡号", "警告");
            }
        }

        //判断输入的不为数字
        //页面加载
        private void PayByCard_Load(object sender, EventArgs e)
        {

        }
        //选择站口
        private void PlatForm(object sender, EventArgs e)
        {

            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=AllPurposeCard;Integrated Security=True");

            string txt = ((Label)sender).Text;

            bool result = Program.PlatFormTXT(txt);
            if (result)
            {
                DBHelper DB = new DBHelper();

                decimal sum;

                string Enter = string.Format(@"select * from Platform where Platform ='{0}'", txt);

                string Exit = string.Format(@"select * from Platform where Platform ='{0}'", txt);

                DataTable TXT_ONE = DB.GetDataTableBySql(Enter);

                DataTable TXT_two = DB.GetDataTableBySql(Exit);

                //获取行中的某列的值：行对象[“列名/索引号”]
                if (EnterPlatform.Text == "")
                {
                    EnterPlatform.Text = TXT_ONE.Rows[0]["Platform"].ToString();
                    ((Label)sender).Enabled = false;
                    MessageBox.Show("请选择出站站台", "提示");
                }
                else
                {
                    ExitPlatform.Text = TXT_two.Rows[0]["Platform"].ToString();
                    if (ExitPlatform.Text != "")
                    {
                        ((Label)sender).Enabled = false;
                    }
                }
                foreach (Control control in this.Platform.Controls)
                {
                    if (control is Label)
                    {
                        if (EnterPlatform.Text != "" && ExitPlatform.Text != "")
                        {
                            control.Enabled = false;
                        }
                    }
                }

            }
        }


        //更改按钮
        private void EnterChange_Click(object sender, EventArgs e)
        {
            EnterPlatform.Text = "";
            ExitPlatform.Text = "";
            Price.Text = "";
            MessageBox.Show("请选择进站站口", "提示");
            foreach (Control control in this.Platform.Controls)
            {
                if (control is Label)
                {
                    if (EnterPlatform.Text == "" && ExitPlatform.Text == "")
                    {
                        control.Enabled = true;
                    }
                }
            }
        }
        //生成消费记录
        private void XpenseTracker_Click(object sender, EventArgs e)
        {
             DBHelper DB = new DBHelper();
            ZhengZeBiaoDa ZZ = new ZhengZeBiaoDa();
            bool result = ZZ.Number(UserCardID.Text);
            if (result)
            {
                for (int i = 0; i < UserCardID.Text.Length; i++)
                {
                    if (char.IsDigit(UserCardID.Text[i]) == false)
                    {
                        MessageBox.Show("请输入合法的卡号", "警告");
                        return;
                    }
                }
                //如果为数字

                string SQL = string.Format(@"select * from UserInformation where UserCardID = '{0}'", UserCardID.Text);
                DataTable inf = DB.GetDataTableBySql(SQL);
                //判断返回标结果集是否为空
                if (inf.Rows.Count != 0)
                {
                    string sql = string.Format(@"select Balance from UserInformation where UserCardID ='{0}'", UserCardID.Text);


                    DataTable yue = DB.GetDataTableBySql(sql);

                    if (Price.Text =="")
                    {
                        MessageBox.Show("请选择站台","警告");
                    }
                    else
                    {
                        decimal shengyu = Convert.ToDecimal(yue.Rows[0]["Balance"].ToString());

                        decimal money = shengyu - Convert.ToDecimal(Price.Text);

                        string update = string.Format(@"update UserInformation set Balance = '{0}' where UserCardID ='{1}'", money, UserCardID.Text);
                        string insert = string.Format(@"insert into ExpenseCalenda(UserCardID,amount,detail,operator)
                    select '{0}','{1}','用于地铁消费','{2}'", UserCardID.Text, Price.Text, UserName.Text);
                        bool result_a = DB.ExcuteSql(update);


                        if (result)
                        {
                            bool add = DB.ExcuteSql(insert);
                            MessageBox.Show("增加成功，当前余额为:" + money.ToString(), "提示");
                            EnterPlatform.Text = "";
                            ExitPlatform.Text = "";
                            Price.Text = ""; 
                            foreach (Control control in this.Platform.Controls)
                            {
                                if (control is Label)
                                {
                                        control.Enabled = true;
                                }
                            }
                        }
                    }
                }
                //未查询到用户
                else
                {
                    MessageBox.Show("为查询到此用户，请输入正确卡号", "提示");
                }
            }
            else
            {
                MessageBox.Show("请输入合法的卡号", "警告");
            }
        }

        //清除数据
        private void ClearData_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("请确认是否删除", "警告", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes)
            {
                foreach (Control con in this.groupBox1.Controls)//遍历控件类集合
                {
                    TextBox tb = con as TextBox;//获取TextBox对象
                    if (tb != null && tb.Text != "")//文本框清空
                    {
                        tb.Text = "";
                    }
                }
                foreach (Control control in this.Platform.Controls)
                {
                    if (control is Label)
                    {
                            control.Enabled = true;
                    }
                }
                MessageBox.Show("删除成功", "提示");
            }
        }

        //获取消费费用
        private void Cost_Click(object sender, EventArgs e)
        {
            DBHelper DB = new DBHelper();

            decimal sum;

            string Enter = string.Format(@"select * from Platform where Platform ='{0}'", EnterPlatform.Text);

            string Exit = string.Format(@"select * from Platform where Platform ='{0}'", ExitPlatform.Text);

            DataTable TXT_ONE = DB.GetDataTableBySql(Enter);

            DataTable TXT_two = DB.GetDataTableBySql(Exit);

            if (TXT_ONE.Rows.Count != 0 &&TXT_two.Rows.Count!=0)
            {
                int entermoney = Convert.ToInt32(TXT_ONE.Rows[0]["PlatformNumber"].ToString());

                int exitmoney = Convert.ToInt32(TXT_two.Rows[0]["PlatformNumber"].ToString());
                sum = Math.Abs((exitmoney - entermoney) * 2);
                Price.Text = sum.ToString();
                MessageBox.Show("请点击生成消费记录", "提示");
            }
            else
            {
                MessageBox.Show("请选择进站/出站站口", "警告");
            }
        }

        private void Inquire_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                e.Handled = false;
            }
        }
        //更改站台按钮
        private void ExitPlatform_TextChanged(object sender, EventArgs e)
        {
            DBHelper DB = new DBHelper();

            decimal sum;

            string Enter = string.Format(@"select * from Platform where Platform ='{0}'", EnterPlatform.Text);

            string Exit = string.Format(@"select * from Platform where Platform ='{0}'", ExitPlatform.Text);
            //获得表结果集
            DataTable TXT_ONE = DB.GetDataTableBySql(Enter);
            //获得表结果集
            DataTable TXT_two = DB.GetDataTableBySql(Exit);
            //返回结果集不为空
            if (TXT_ONE.Rows.Count != 0 && TXT_two.Rows.Count != 0)
            {
                int entermoney = Convert.ToInt32(TXT_ONE.Rows[0]["PlatformNumber"].ToString());

                int exitmoney = Convert.ToInt32(TXT_two.Rows[0]["PlatformNumber"].ToString());
                sum = Math.Abs((exitmoney - entermoney) * 2);
                Price.Text = sum.ToString();
            }
            else
            {
                MessageBox.Show("请选择进站/出站站口", "警告");
            }
        }
        
        private void PayByCard_FormClosing(object sender, FormClosingEventArgs e)
        {

            e.Cancel = true;  //取消该事件响应

            this.Visible = false; //隐藏
        }
    }
    
}

